﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServerManager.Classes;
using System.Reflection;
using System.Net.Sockets;
using ServerManager.Network;
using System.Xml.Serialization;
using System.Collections;

namespace ServerManager
{
    public partial class frmMain : Form
    {
        private List<Type> types = new List<Type>();
        private Type selectedType;
        public DrachamNetwork network;
        private String sessionKey;
        private List<Object> objects = new List<object>();

        public frmMain()
        {
            InitializeComponent();

            foreach (Control c in this.scControls.Panel1.Controls)
            {
                c.Enabled = false;
            } 

            Assembly asm = Assembly.GetExecutingAssembly();
            foreach (Type t in asm.GetTypes())
            {
                if (t.Namespace == "ServerManager.Classes")
                {
                    this.cobObjects.Items.Add(t.Name);
                    this.types.Add(t);
                }
            }

            if (this.cobObjects.Items.Count > 0)
                this.cobObjects.SelectedIndex = 0;
        }

        private void miConnectTo_Click(object sender, EventArgs e)
        {
            frmConnect frm = new frmConnect();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.network = frm.Network;
                this.sessionKey = frm.SessionKey;
                this.statLblConnected.Text = "Connected";

                foreach (Control c in this.scControls.Panel1.Controls)
                {
                    c.Enabled = true;
                }
            }
        }

        private void cobObjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.network == null) return;

            Type type = this.types.First(t => t.Name == (sender as ComboBox).SelectedItem.ToString());
            this.selectedType = type;

            ServerList sl = new ServerList(this.network);
            sl.OnListReceived += new ReceivedServerList(OnListReceived);

            this.network.Invoke(MessageType.CXmlDataRequest,
                new NetworkParameter(this.sessionKey, 36),
                new NetworkParameter(type.Name, 30));
        }

        private void OnListReceived(IList list)
        {
            if (this.gvObjects.InvokeRequired)
            {
                ReceivedServerList rsl = new ReceivedServerList(
                    delegate(IList l)
                    {
                        this.gvObjects.DataSource = l;
                    });

                this.gvObjects.Invoke(rsl, list);
            }
            else
            {
                this.gvObjects.DataSource = list;
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Type type = this.types.First(t => t.Name == this.cobObjects.SelectedItem.ToString());
            
            frmManageObject fMO = new frmManageObject(type.GetConstructors().First().Invoke(null));
            if (fMO.ShowDialog() == DialogResult.OK)
            {
                this.cobObjects_SelectedIndexChanged(this.cobObjects, new EventArgs());
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.gvObjects.Rows.Count > 0)
            {
                if (this.gvObjects.SelectedRows.Count == 0) return;

                frmManageObject fMO = new frmManageObject(this.gvObjects.SelectedRows[0].DataBoundItem);
                if (fMO.ShowDialog() == DialogResult.OK)
                {
                    this.cobObjects_SelectedIndexChanged(this.cobObjects, new EventArgs());
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            // order server to delete item
            // remove from own list

            this.cobObjects_SelectedIndexChanged(this.cobObjects, new EventArgs());
        }
    }
}
